<?php
/**
 * AdMinLang is a class used for translations in AdMin
 *
 * Here is an example of usage
 * <code>
 * require_once(DIR_CLASSES ."/AdMinLang.php");
 *
 * $lang = new AdMinLang($module->id, 'es');
 * $lang->translate("hello_world");
 * </code>
 *
 * @package AdMin Framework
 */
class adminLang
{
    public $moduleID = '';
    public $lang = '';
    public $words = array();
    private $langLoaded = false;
    private $sessionManager;

    public function __construct($moduleID='')
    {
        $this->sessionManager = new SessionManager();
        $this->moduleID = $moduleID;
    }

    public function setLang($lang)
    {
        $this->lang = $lang;
        $this->sessionManager->set('__AdMinLang__', $this->lang);
    }

    public function translate($word)
    {
        if (!$this->langLoaded) $this->loadLang(); // Always loads admin moduleID
        $str = $this->words[$word]? $this->words[$word] : $word;
        return $str;
    }

    private function getLang()
    {
        if(!$this->lang)
        {
            if(!$this->lang = $this->sessionManager->get('__AdMinLang__'))
                $this->setLang(($GLOBALS["CONF"]["lang"]["default"]? $GLOBALS["CONF"]["lang"]["default"] : 'es')); // backward compatibility
        }
    }

    public function loadLang()
    {
        $this->getLang();
        if(!$this->lang) die("Falta definir lang");

        $AdMinfile  = DIR_ADMIN ."/admin/lang/". $this->lang .".php";
       
        $moduleFile = "";
        if( $this->moduleID )
        {
            if( file_exists(DIR_MODULES ."/". $this->moduleID ."/lang/". $this->lang .".php") )
            {
                $moduleFile = DIR_MODULES ."/". $this->moduleID ."/lang/". $this->lang .".php";
            }
            else
            {
                $moduleFile = DIR_ADMIN ."/". $this->moduleID ."/lang/". $this->lang .".php";
            }
        }
		
        if(!file_exists($AdMinfile) && $moduleFile=="" ) die("error al cargar el archivo lang");
        
        require_once($AdMinfile);
        $this->words = $words;
        if (file_exists($moduleFile))
        {
            require_once($moduleFile);
            $this->words = array_merge($this->words, $words);
        }
        $this->langLoaded = true;
    }
}

?>
